/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.serviet;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: javaweb - T2
 * <p>Powered by webrx On 2022-03-15 09:34:08
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@WebServlet(urlPatterns = {"/api/t3"}) @MultipartConfig
public class T3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getMethod());

        System.out.println(req.getParameter("id"));
        System.out.println(req.getParameter("name"));

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 222);
        map.put("msg", "注册成功");
        out.print(JSON.toJSON(map));

        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //System.out.println(req.getReader().readLine());

        System.out.println(req.getMethod());
        System.out.println(req.getParameter("id"));
        System.out.println(req.getParameter("name"));
        System.out.println(req.getParameter("addr"));

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "注册成功");
        out.print(JSON.toJSON(map));

        out.flush();
        out.close();


    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ajax post 请求负载（字符串），没有乱码
        System.out.println(req.getReader().readLine());

        //表单post请求中文文字
        req.setCharacterEncoding("utf-8");


        System.out.println(req.getMethod());
        System.out.println(req.getParameter("id"));
        System.out.println(req.getParameter("name"));
        System.out.println(req.getParameter("addr"));

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "PUT注册成功");
        out.print(JSON.toJSON(map));

        out.flush();
        out.close();

    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ajax post 请求负载（字符串），没有乱码
        System.out.println(req.getReader().readLine());

        //表单post请求中文文字
        req.setCharacterEncoding("utf-8");


        System.out.println(req.getMethod());
        System.out.println(req.getParameter("id"));
        System.out.println(req.getParameter("name"));
        System.out.println(req.getParameter("addr"));

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "delete注册成功");
        out.print(JSON.toJSON(map));

        out.flush();
        out.close();
    }
}
